package org.water.sys.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.water.base.entity.ResultBaseBean;
import org.water.base.web.BaseController;
import org.water.common.bind.annotation.MethodDesc;
import org.water.sys.entity.SysGroupRole;
import org.water.sys.entity.SysGroupUser;
import org.water.sys.entity.SysRole;
/**
 * 用户组管理
 * @author qzy
 *
 */
import org.water.sys.entity.SysGroup;
import org.water.sys.service.RoleService;
import org.water.sys.service.UserGroupService;

import com.alibaba.fastjson.JSONObject;
@Controller
@RequestMapping("/admin/group")
public class UserGroupController extends BaseController {

	/**
	 * 用户组service
	 */
	@Autowired
	private UserGroupService userGroupService;
	@Autowired
	private RoleService roleService;
	/**
	 * 用户组主页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response)
	{
		
		return "admin/userGroup/userGroupManager";
	}
	/**
	 * 查询用户组
	 * @param groupName
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@MethodDesc(name="用户组管理",value="查看用户组列表")
	@RequestMapping("/qry")
	public Page<Map> findByGroupName(@RequestParam("name") String groupName,@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize)
	{
		return userGroupService.findByGroupName(groupName, new PageRequest(pageIndex, pageSize));
	}
	/**
	 * 查询单个用户组信息
	 * @param groupId
	 * @return
	 */
	@RequestMapping("/one")
	public SysGroup findOne(@RequestParam("id") String groupId)
	{
		return userGroupService.findOne(groupId);
	}
	
	/**
	 * 根据用户组查询用户
	 * @param groupId
	 * @return
	 */
	@MethodDesc(name="用户组管理",value="查询用户组关联的用户")
	@RequestMapping("/user/qry")
	public List<Map> findUserByGroupId(@RequestParam("groupId") String groupId)
	{
		return userGroupService.findUserByGroupId(groupId);
	}
	/**
	 * 保存
	 * @param sysUserGroup
	 * @return
	 */
	@MethodDesc(name="用户组管理",value="保存用户组")
	@RequestMapping("/save")
	
	public ResultBaseBean save(SysGroup sysUserGroup)
	{
		userGroupService.save(sysUserGroup);
		return getBaseBean(true, "");
	}
	
	/**
	 * 查询不包含在用户组中的角色
	 * @param groupId
	 * @return
	 */
	@RequestMapping("/role")
	public List<SysRole> findRoleNotInGroup(@RequestParam("groupId") String groupId)
	{
		List<SysRole> list=userGroupService.findRoleNotInGroup(groupId);
		return list;
	}
	/**
	 * 所有角色和用户组关联的角色
	 * @param groupId
	 * @return
	 */
	@RequestMapping("/role/all")
	public ResultBaseBean findGroupRoles(@RequestParam("groupId") String groupId)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		HashMap<String, Object> data=new HashMap<>();
		ArrayList<String> selectRoles=new ArrayList<>();
		List<SysRole> roles=roleService.findAllRole();
		List<SysGroupRole> grs=userGroupService.findGroupRole(groupId);
		if(grs!=null && !grs.isEmpty())
		{
			Iterator<SysGroupRole> iterator=grs.iterator();
			while(iterator.hasNext())
			{
				SysGroupRole gr=iterator.next();
				selectRoles.add(gr.getRoleId());
			}
		}
		data.put("roles", roles);
		data.put("selectRoles", selectRoles.toArray());
		bean.setData(data);
		return bean;
	}
	/**
	 * 保存用户组关联角色
	 * @param groupRoles
	 */
	@MethodDesc(name="用户组管理",value="保存用户组关联角色")
	@RequestMapping("/role/save")
	public ResultBaseBean saveUserGroupRoles(@RequestParam("groupId") String groupId,@RequestParam("roleIds") String roleIds)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		List<SysGroupRole> groupRoles=new ArrayList<>();
		if(roleIds!=null || !"".equals(roleIds))
		{
			String[] roleIdArr=roleIds.split(",");
			for(String roleId:roleIdArr)
			{
				SysGroupRole groupRole=new SysGroupRole();
				groupRole.setGroupId(groupId);
				groupRole.setRoleId(roleId);
				groupRoles.add(groupRole);
			}
			userGroupService.saveUserGroupRoles(groupId, groupRoles);
		}
		else
		{
			bean.setSuccess(false);
			bean.setMsg("用户组未关联角色，保存失败");
		}
		return bean;
	}
	/**
	 * 查询不包含在用户组中的用户,用于组关联用户
	 * @param groupId
	 * @return
	 */
	@RequestMapping("/user/rel")
	public Page<Map> findUserNotInGroup(@RequestParam("groupId") String groupId,@RequestParam("userName") String userName,
			@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize)
	{
		Page<Map> result=userGroupService.findUserNotInGroup(groupId,userName,new PageRequest(pageIndex, pageSize));
		return result;
	}
	/**
	 * 保存用户组关联用户
	 * @param groupUsers
	 */
	@MethodDesc(name="用户组管理",value="保存用户组关联用户")
	@RequestMapping("/user/save")
	public ResultBaseBean saveUserGroupUsers(@RequestParam("groupId") String groupId,@RequestParam("userIds") String userIds)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		List<SysGroupUser> groupUsers=new ArrayList<>();
		if(userIds!=null || !"".equals(userIds))
		{
			String[] userIdArr=userIds.split(",");
			for(String userId:userIdArr)
			{
				SysGroupUser groupRole=new SysGroupUser();
				groupRole.setGroupId(groupId);
				groupRole.setUserId(userId);
				groupUsers.add(groupRole);
			}
			userGroupService.saveUserGroupUsers(groupUsers);
		}
		else
		{
			bean.setSuccess(false);
			bean.setMsg("用户组未关联用户,保存失败");
		}
		return bean;
	}
	
	/**
	 * 删除用户组关联的用户
	 * @param ids
	 */
	@MethodDesc(name="用户组管理",value="删除用户组关联的用户")
	@RequestMapping("/user/del")
	public ResultBaseBean deleteUserGroupUser(@RequestParam("ids") String ids)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		if(ids!=null || !"".equals(ids))
		{
			userGroupService.deleteUserGroupUser(ids.split(","));
		}
		else
		{
			bean.setSuccess(false);
			bean.setMsg("用户组关联用户为空,删除失败");
		}
		return bean;
	}
	/**
	 * 删除用户组
	 * @param groupId
	 */
	@MethodDesc(name="用户组管理",value="删除用户组")
	@RequestMapping("/del")
	public ResultBaseBean delUserGroup(@RequestParam("id") String groupId)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		if(userGroupService.hasGroupRelUser(groupId))
		{
			bean.setSuccess(false);
			bean.setMsg("用户组已关联用户");
		}
		else if(userGroupService.hasGroupRelRole(groupId))
		{
			bean.setSuccess(false);
			bean.setMsg("用户组已关联角色");
		}
		else
		{
			userGroupService.del(groupId);
		}
		
		return bean;
	}
	
}
